<template>
  <div>
    <div class="chooseApply">
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('申请标题')" prop="title">
          <el-input
            v-model="queryParams.title"
            size="small"
            :placeholder="$t('申请标题')"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item :label="$t('流程名称')" prop="key">
          <el-select v-model="queryParams.key" size="small" :placeholder="$t('流程名称')" filterable @visible-change="$forceUpdate()">
            <el-option
              v-for="process in procDefKeyOptions"
              :key="process.key"
              :label="process.name"
              :value="process.key"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('申请时间')">
          <DatePicker v-model="dateRange" type="daterange" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        tooltip-effect="darkTable"
        :data="applyList"
        border
        class="nowrapTable"
        max-height="450"
        row-key="id"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="40" align="center" reserve-selection />
        <el-table-column :label="$t('申请标题')" align="left" prop="title" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-link type="primary" @click="openTask(scope.row)">{{ scope.row.title }}</el-link>
          </template>
        </el-table-column>
        <el-table-column :label="$t('申请时间')" align="center" prop="beginTime" class-name="small-padding fixed-width" width="150" />
        <el-table-column :label="$t('结束时间')" align="center" prop="endTime" class-name="small-padding fixed-width" width="150" />
        <el-table-column :label="$t('流程名称')" align="center" prop="key" class-name="small-padding fixed-width" :formatter="processFormat" width="150" />
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <div slot="footer" class="dialog-footer" style="text-align: right;margin-top: 10px">
      <div class="saveBtnAll">
        <el-button @click="cancel()">{{ $t("取消") }}</el-button>
        <el-button type="primary" @click="confirmForm()">{{ $t("保存") }}</el-button>
      </div>
    </div>

    <el-dialog :visible.sync="formOpen" :title="$t('任务详情')" custom-class="dialogForm" :close-on-click-modal="false" :lock-scroll="false" :destroy-on-close="true" append-to-body @close="isFormBtn">
      <ProcessInfoForm ref="processInfoForm" />
    </el-dialog>

  </div>
</template>

<script>
  import { applyList, listDataProcess } from '@/api/act/process/actProcess'
  import ProcessInfoForm from '../process/processInfoForm'
export default {
  components: {
    ProcessInfoForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 待办表格数据
      applyList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      // 查询条件的时间区间
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        applyUserId: '',
        processName: undefined,
        status: 'W'
      },
      wfMenuProcessData: {},
      // 表单参数
      form: {},
      // 流程下拉框
      procDefKeyOptions: [],
      // 选中的流程
      chooseProcess: []
    }
  },
  created() {
    listDataProcess().then(response => {
      this.procDefKeyOptions = response.data
    })
  },
  methods: {
    /** 查询待办列表 */
    getList() {
      this.loading = true
      applyList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.applyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.$parent.chooseApplyOpen = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = []
      this.handleQuery()
    },
    // 打开流程详情页面
    openTask(row) {
      this.formOpen = true
      this.$nextTick(() => {
        row.actId = row.id
        this.$refs.processInfoForm.init(row)
      })
    },
    processFormat(row) {
      var actions = []
      Object.keys(this.procDefKeyOptions).map((key) => {
        if (this.procDefKeyOptions[key].key === ('' + row.key)) {
          actions.push(this.procDefKeyOptions[key].name)
          return false
        }
      })
      return actions.join('')
    },
    // 复选框点击事件
    handleSelectionChange(selection) {
      this.chooseProcess = selection.map(item => item)
    },
    // 确认操作
    confirmForm() {
      this.$parent.$parent.$parent.associatedProcess = this.chooseProcess
      this.$parent.$parent.$parent.chooseApplyOpen = false
    },
    // 初始化数据
    init(associatedProcess) {
      this.chooseProcess = associatedProcess
      if (this.chooseProcess.length === 0) {
        this.$refs.multipleTable.store.clearSelection()
      }
      this.getList()
    },
    isFormBtn() {
      // 表单配置刷新流程按钮
      this.$store.dispatch('formBtn/toggleForcedBtnHide', false)
    }
  }
}
</script>
<style scoped>
.dialogForm .el-dialog__body>div .el-form {
  height: auto;
  overflow: auto;
  position: inherit;
  top: 0;
  bottom: 40px;
  right: -20px;
  padding-right: 20px;
  left: 0;
  padding-bottom: 10px;
}
</style>
